//
// Created by ins on 2021/2/27.
//

#include <PlayerBean.h>
#include <json.h>
#include <string>
#include <list>

#include "Util.h"
#include "PlayerPresenter.h"
#include "PlayerDao.h"



using namespace std;
PlayerDao *playerDao;
void addPlayer(string data, ResData& res_data);
void delPlayer(string data, ResData& res_data);
void updatePlayer(string data, ResData& res_data);
void getPlayer(string data, ResData& res_data);
void updatePlayerEndTime(string data, ResData& res_data);
void changeExp(string data, ResData& res_data);
void updatePlayerSX(string data, ResData& res_data);
void getAllPlayer(string data, ResData& res_data);
void updateIsOnline(string data, ResData& res_data);
moduleHandler player_cmds[] = {
        {"01", addPlayer},
        {"02", delPlayer},
        {"03", updatePlayer},
        {"04", getPlayer},
        {"05", updatePlayerEndTime},
        {"06", changeExp},
        {"07", updatePlayerSX},
        {"08", getAllPlayer},
        {"09", updateIsOnline}

};

void addPlayer(string data, ResData& res_data){}
void delPlayer(string data, ResData& res_data){}
void updatePlayer(string data, ResData& res_data){}
void getPlayer(string data, ResData& res_data){
    PlayerBean cxplayer;
    
    Json::Value root = Util::init_root(data);
    if(!root["sid"].isNull()){
        string sid = root["sid"].asString();

        playerDao->getPlayer(sid, cxplayer);

        if(cxplayer.getSid() == ""){
            res_data.resp_code = ERRCD_NOPLAYER;
        }else{
            res_data.resp_code = ERRCD_PASS;
            res_data.resp_data = "\"player\":" + cxplayer.toString();
        }
    }
}
void updatePlayerEndTime(string data, ResData& res_data){

    Json::Value root = Util::init_root(data);
    if(!root["sid"].isNull()){
        string sid = root["sid"].asString();
        if(playerDao->updatePlayerEndTime(sid)){
            res_data.resp_code = ERRCD_PASS;
            res_data.resp_data = OKACKDATA;
        }else{
            res_data.resp_code = ERRCD_FALSE;
            res_data.resp_data = ERRACKDATA;
        }
    }
}
void changeExp(string data, ResData& res_data){

    PlayerBean player;
    Json::Value root = Util::init_root(data);

    if(!root["sid"].isNull()){
        string sid = root["sid"].asString();
        string exp = root["exp"].asString();

        playerDao->getPlayer(sid, player);
        if(atoi(player.getExp().c_str()) > atoi(player.getMaxExp().c_str())){
            return;
        }
        playerDao->changeExp(sid, exp);
    }

}
void updatePlayerSX(string data, ResData& res_data){

    Json::Value root = Util::init_root(data);

    if(!root["sid"].isNull()){
        string sid = root["sid"].asString();
        string mibName = root["mibName"].asString();
        string mibValue = root["mibValue"].asString();

        if(playerDao->updatePlayerSX(sid, mibName, mibValue)){
            res_data.resp_code = ERRCD_PASS;
            res_data.resp_data = OKACKDATA;
        }else{
            res_data.resp_code = ERRCD_FALSE;
            res_data.resp_data = ERRACKDATA;
        }
    }
}
void getAllPlayer(string data, ResData& res_data){

    list<PlayerBean> allPlayers;
    Json::Value root = Util::init_root(data);
    if(!root["mid"].isNull()) {
        string nowmid = root["mid"].asString();
        playerDao->getAllPlayer(nowmid, allPlayers);

        if(allPlayers.size() != 0){
            //TODO
            res_data.resp_code = ERRCD_NONE;
            res_data.resp_data = NONEACKDATA;
        } else{
            res_data.resp_code = ERRCD_NONE;
            res_data.resp_data = NONEACKDATA;
        }
    }
}
void updateIsOnline(string data, ResData& res_data){

}

PlayerPresenter::PlayerPresenter() {
    playerDao = PlayerDao::getInstance();
}
PlayerPresenter::~PlayerPresenter() {}
void PlayerPresenter::PlayerMsgHandler(string cmd, string data, ResData &outData) {
    cout << "player: " << data << endl;
    int i = 0;

    if(cmd.empty() || data.empty()){
        return;
    }

    for (i = 0; !player_cmds[i].cmd.empty(); ++i)
    {
        if (cmd != player_cmds[i].cmd)
            continue;
        break;
    }

    player_cmds[i].func(data, outData);
}